<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>优惠券</title>
    <style>
        div {
            position: absolute;
            top: 30%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
        }

        table, tr, th, td {
            border: 1px solid #5fbdcb;
        }

        table {
            border-collapse: collapse;
        }
    </style>
</head>
<body>
<div>
    <table></table>
    <br>
    <a href="">添加优惠券</a><br>
    <a href="SellerStorePage.jsp">返回</a>
</div>
<script src="https://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
<script>
    function getParam(url, name) {
        try {
            let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            let r = url.split('?')[1].match(reg);
            if (r != null) {
                return r[2];
            }
            return "";
        } catch (e) {
            return "";
        }
    }

    let url = window.location.href;
    let storeId = getParam(url, 'storeId');
    $(function () {
        let a = document.querySelector('a');
        a.href = 'AddCoupon.jsp?storeId=' + storeId;
        $.ajax({
            url: "http://localhost:8080/coupon.do",
            type: "post",
            data: {
                storeId: storeId,
                method: 'selectCoupon',
            },
            dataType: "text",
            success: function (data) {
                let table = document.querySelector('table');
                let parse = JSON.parse(data);
                if (parse.length === 0) {
                    alert("你的店铺还没有优惠券");
                } else {
                    for (let i = 0; i < parse.length; i++) {
                        let tr = document.createElement('tr');
                        table.appendChild(tr);
                        let th1 = document.createElement('th');
                        th1.innerText = '优惠券面值';
                        tr.appendChild(th1);
                        let td1 = document.createElement('td');
                        td1.innerText = parse[i].value;
                        tr.appendChild(td1);
                        let th2 = document.createElement('th');
                        th2.innerText = '最低消费';
                        tr.appendChild(th2);
                        let td2 = document.createElement('td');
                        td2.innerText = parse[i].min;
                        tr.appendChild(td2);
                        let th3 = document.createElement('th');
                        th3.innerText = '有效时间';
                        tr.appendChild(th3);
                        let td3 = document.createElement('td');
                        td3.innerText = parse[i].validity;
                        tr.appendChild(td3);
                        let td4 = document.createElement('td');
                        tr.appendChild(td4);
                        let a1 = document.createElement('a');
                        a1.innerText = '删除';
                        a1.href = 'DeleteCoupon.jsp?couponId=' + parse[i].couponId + '&storeId=' + storeId;
                        td4.appendChild(a1);
                        let td5 = document.createElement('td');
                        tr.appendChild(td5);
                        let a2 = document.createElement('a');
                        a2.innerText = '修改';
                        a2.href = 'ModifyCoupon.jsp?couponId=' + parse[i].couponId + '&storeId=' + storeId;
                        td5.appendChild(a2);
                    }
                }
            }
        })
    })
</script>
</body>
</html>
